import type {WL_CoreInterface, WL_LocationReportInfo} from "@/core/weila_datas";
import {WL_ExtEventID} from "@/core/weila_datas";
import {WL} from "@/proto/weilapb";
import type {WL_IDbLocationInfo} from "@/proto/weilapb_external_data";
import type {WLBuildMsgRet, WLSessionInfo} from "@/proto/weilapb_wrapper_data";
import Long from "long";
import {WeilaPbLocationWrapper} from "@/proto/weilapb_location_wrapper";
import debug from "debug";

const LLOG = debug("LocationLog")

export default class WLLocationModule {
	constructor(private coreInterface: WL_CoreInterface) {
		this.coreInterface.registerPbMsgHandler(WL.Service.ServiceID.SERVICE_LOCATION, this.onPbMsgHandler.bind(this));
	}

	private onPbMsgHandler(data: any): void {
		const serverMessage = data as WL.Service.IServiceMessage;
		const serviceHead = serverMessage.serviceHead;
		if (serviceHead!.commandType == WL.Service.CommandType.COMMAND_RESPONSE) {
			if (serviceHead!.commandId == WL.Location.LocationCommandId.LOCATION_COMMAND_GET_LOCATION) {
				LLOG('获取的消息结果 %o', serverMessage);
				this.coreInterface.rspPbMsg(serviceHead!.seq || 0, serviceHead!.resultCode || 0, serverMessage!.locationMessage!.rspGetLocation);
			}else if (serviceHead!.commandId == WL.Location.LocationCommandId.LOCATION_COMMAND_REPORT_LOCATION) {
				this.coreInterface.rspPbMsg(serviceHead!.seq || 0, serviceHead!.resultCode || 0, true);
			}
		}else if (serviceHead!.commandType == WL.Service.CommandType.COMMAND_NOTIFY) {
			if (serviceHead!.commandId == WL.Location.LocationCommandId.LOCATION_COMMAND_LOCATION_CONTROL) {
				const notify = serverMessage.locationMessage!.ntfLocationControl!;
				const reportInfo = {} as WL_LocationReportInfo;
				reportInfo.sessionId = (notify.sessionInfo!.sessionId || '0').toString();
				reportInfo.sessionType = notify.sessionInfo!.sessionType || 0;
				reportInfo.status = notify.locationControl!.status || false;
				reportInfo.duration = notify.locationControl!.duration || 0;
				reportInfo.frequency = notify.locationControl!.frequency || 0;
				this.coreInterface.sendExtEvent(WL_ExtEventID.WL_EXT_LOCATION_CTRL_IND, reportInfo);
			}
		}
	}

	public async getLocation(sessionId: string, sessionType: number): Promise<WL_IDbLocationInfo[]> {
		const sessionInfo = {} as WLSessionInfo;
		sessionInfo.sessionId = Long.fromValue(sessionId);
		sessionInfo.sessionType = sessionType;
		const getLocationReq: WLBuildMsgRet = WeilaPbLocationWrapper.buildGetLocationReq(sessionInfo, []);

		if (getLocationReq.resultCode == 0) {
			try {
				const rspLocation: WL.Location.IRspGetLocation = await this.coreInterface.sendPbMsg(getLocationReq);
				const locationInfos: WL_IDbLocationInfo[] = [];

				for (const info of rspLocation.locationInfos!) {
					const locationInfo: WL_IDbLocationInfo = {} as WL_IDbLocationInfo;
					locationInfo.userId = info.userId || 0;
					locationInfo.clientType = info.clientType || 0;
					locationInfo.locationType = info.locationType || 0;
					locationInfo.altitude = info.locationParam!.altitude || 0;
					locationInfo.longitude = info.locationParam!.longitude || 0;
					locationInfo.latitude = info.locationParam!.latitude || 0;
					locationInfo.radius = info.locationParam!.radius || 0;
					locationInfo.speed = info.locationParam!.speed || 0;
					locationInfo.direction = info.locationParam!.direction || 0;
					locationInfo.timestamp = Long.fromValue(info.locationParam!.timestamp || 0).toNumber();

					locationInfos.push(locationInfo);
				}

				return locationInfos;
			}catch (e) {
				return Promise.reject('获取位置错误' + e);
			}
		}


		return Promise.reject('创建消息失败:' + getLocationReq.resultCode);
	}
}
